home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / dvips.new / RCS / dvips.c,v < prev   
Encoding:
Text File  |  1991-01-15  |  17.9 KB  |  597 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    tve:1.1; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     91.01.14.16.03.42;  author tve;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @original
  17. @
  18.  
  19.  
  20.  
  21. 1.1
  22. log
  23. @Initial revision
  24. @
  25. text
  26. @/*
  27.  *   This is the main routine.
  28.  */
  29. #ifndef DEFRES
  30. #define DEFRES (300)
  31. #endif
  32.  
  33. #include "structures.h" /* The copyright notice there is included too! */
  34. #ifdef VMS
  35. #define GLOBAL globaldef
  36. #include climsgdef
  37. #include ctype
  38. #include descrip
  39. #endif
  40. /*
  41.  *   First we define some globals.
  42.  */
  43. #ifdef VMS
  44.     static char ofnme[252],infnme[252],pap[40],thh[20];
  45. #endif
  46. fontdesctype *fonthead ;      /* list of all fonts mentioned so far */
  47. fontdesctype *curfnt ;        /* the currently selected font */
  48. sectiontype *sections ;       /* sections to process document in */
  49. Boolean manualfeed ;          /* manual feed? */
  50. Boolean compressed ;          /* compressed? */
  51. Boolean safetyenclose ;       /* enclose in save/restore for stupid spoolers? */
  52. Boolean removecomments ;      /* remove comments from included PS? */
  53. Boolean nosmallchars ;        /* disable small char optimization for X4045? */
  54. int collatedcopies = 1 ;      /* how many collated copies? */
  55. int sectioncopies = 1 ;       /* how many times to repeat each section? */
  56. shalfword linepos = 0 ;       /* where are we on the line being output? */
  57. integer maxpages ;            /* the maximum number of pages */
  58. Boolean notfirst, notlast ;   /* true if a first page was specified */
  59. Boolean sendcontrolD ;        /* should we send a control D at end? */
  60. integer firstpage ;           /* the number of the first page if specified */
  61. integer lastpage ;
  62. integer firstseq ;
  63. integer lastseq ;
  64. int numcopies ;               /* number of copies of each page to print */
  65. char *oname ;                 /* output file name */
  66. char *iname ;                 /* dvi file name */
  67. char *strings ;               /* strings for program */
  68. char *nextstring, *maxstring ; /* string pointers */
  69. FILE *dvifile, *bitfile ;     /* dvi and output files */
  70. quarterword *curpos ;         /* current position in virtual character packet */
  71. quarterword *curlim ;         /* final byte in virtual character packet */
  72. fontmaptype *ffont ;          /* first font in current frame */
  73. real conv ;                   /* conversion ratio, pixels per DVI unit */
  74. real vconv ;                  /* conversion ratio, pixels per DVI unit */
  75. real alpha ;                  /* conversion ratio, DVI unit per TFM unit */
  76. integer mag ;                 /* the magnification of this document */
  77. Boolean overridemag ;         /* substitute for mag value in DVI file? */
  78. int actualdpi = DEFRES ;      /* the actual resolution of the printer */
  79. int vactualdpi = DEFRES ;      /* the actual resolution of the printer */
  80. int maxdrift ;                /* max pixels away from true rounded position */
  81. int vmaxdrift ;                /* max pixels away from true rounded position */
  82. char *paperfmt ;              /* paper format */
  83. int landscape = 0 ;           /* landscape mode */
  84. integer fontmem ;             /* memory remaining in printer */
  85. integer pagecount ;           /* page counter for the sections */
  86. integer pagenum ;             /* the page number we currently look at */
  87. long bytesleft ;              /* number of bytes left in raster */
  88. quarterword *raster ;         /* area for raster manipulations */
  89. integer hh, vv ;              /* horizontal and vertical pixel positions */
  90. char *tfmpath = TFMPATH ;     /* pointer to directories for tfm files */
  91. char *pkpath = PKPATH ;       /* pointer to directories for pk files */
  92. char *vfpath = VFPATH ;       /* pointer to directories for vf files */
  93. char *figpath = FIGPATH ;     /* pointer to directories for figure files */
  94. char *headerpath = HEADERPATH ; /* pointer to directories for header files */
  95. char *configpath = CONFIGPATH;  /* where to find config files */
  96. #ifdef SEARCH_SUBDIRECTORIES
  97. char *fontsubdirpath = FONTSUBDIRPATH ;
  98. #endif
  99. #ifdef FONTLIB
  100. char *flipath = FLIPATH ;     /* pointer to directories for fli files */
  101. char *fliname = FLINAME ;     /* pointer to names of fli files */
  102. #endif
  103. integer swmem ;               /* font memory in the PostScript printer */
  104. int quiet ;                   /* should we only print errors to stderr? */
  105. int filter ;                  /* act as filter default output to stdout,
  106.                                                default input to stdin? */
  107. int prettycolumn ;            /* the column we are at when running pretty */
  108. int totalpages = 0 ;          /* total number of pages */
  109. Boolean reverse ;             /* are we going reverse? */
  110. Boolean usesPSfonts ;         /* do we use local PostScript fonts? */
  111. Boolean usesspecial ;         /* do we use \special? */
  112. Boolean headers_off ;         /* do we send headers or not? */
  113. char *headerfile ;            /* default header file */
  114. char *warningmsg ;            /* a message to write, if set in config file */
  115. Boolean multiplesects ;       /* more than one section? */
  116. Boolean disablecomments ;     /* should we suppress any EPSF comments? */
  117. char *printer ;               /* what printer to send this to? */
  118. char *mfmode ;                /* default MF mode */
  119. frametype frames[MAXFRAME] ;  /* stack for virtual fonts */
  120. fontdesctype *baseFonts[256] ; /* base fonts for dvi file */
  121. integer pagecost;               /* memory used on the page being prescanned */
  122. int delchar;                    /* characters to delete from prescanned page */
  123. integer fsizetol;               /* max dvi units error for psfile font sizes */
  124. Boolean includesfonts;          /* are fonts used in included psfiles? */
  125. fontdesctype *fonthd[MAXFONTHD];/* list headers for included fonts of 1 name */
  126. int nextfonthd;                 /* next unused fonthd[] index */
  127. char xdig[256];                 /* table for reading hexadecimal digits */
  128. char banner[] = BANNER ;        /* our startup message */
  129. extern int dontmakefont ;
  130. #ifdef DEBUG
  131. integer debug_flag = 0;
  132. #endif /* DEBUG */
  133. /*
  134.  *   This routine calls the following externals:
  135.  */
  136. extern void outbangspecials() ;
  137. extern void prescanpages() ;
  138. extern void initprinter() ;
  139. extern void cleanprinter() ;
  140. extern void dosection() ;
  141. extern void getdefaults() ;
  142. extern void cmdout() ;
  143. extern void numout() ;
  144. extern int add_header() ;
  145. extern char *strcpy() ;
  146. extern void checkenv() ;
  147. #ifdef FONTLIB
  148. extern void fliload() ;
  149. #endif
  150. /*
  151.  *   This error routine prints an error message; if the first
  152.  *   character is !, it aborts the job.
  153.  */
  154. static char *progname ;
  155. void
  156. error(s)
  157.     char *s ;
  158. {
  159.    extern void exit() ;
  160.  
  161.    (void)fprintf(stderr, "%s: %s\n", progname, s) ;
  162.    if (*s=='!') {
  163.       if (bitfile != NULL) {
  164.          cleanprinter() ;
  165.       }
  166.       exit(1) ; /* fatal */
  167.    }
  168. }
  169.  
  170. /*
  171.  *   Initialize sets up all the globals and data structures.
  172.  */
  173. void
  174. initialize()
  175. {
  176.    extern char *malloc() ;
  177.    int i;
  178.    char *s;
  179.  
  180.    nextfonthd = 0;
  181.    for (i=0; i<256; i++)
  182.       xdig[i] = 0;
  183.    i = 0;
  184.    for (s="0123456789ABCDEF"; *s!=0; s++)
  185.       xdig[*s] = i++;
  186.    i = 10;
  187.    for (s="abcdef"; *s!=0; s++)
  188.       xdig[*s] = i++;
  189.    strings = malloc(STRINGSIZE) ;
  190.    if (strings == 0)
  191.       error("! no memory for strings") ;
  192.    maxpages = 100000 ;
  193.    numcopies = 1 ;
  194.    nextstring = strings ;
  195.    iname = strings ;
  196.    *nextstring++ = 0 ;
  197.    maxstring = strings + STRINGSIZE - 200 ;
  198.    bitfile = NULL ;
  199.    bytesleft = 0 ;
  200.    swmem = SWMEM ;
  201.    oname = OUTPATH ;
  202.    sendcontrolD = 0 ;
  203.    multiplesects = 0 ;
  204.    disablecomments = 0 ;
  205.    maxdrift = -1 ;
  206.    vmaxdrift = -1 ;
  207. }
  208. /*
  209.  *   This routine copies a string into the string `pool', safely.
  210.  */
  211. char *
  212. newstring(s)
  213.    char *s ;
  214. {
  215.    int l ;
  216.  
  217.    if (s == NULL)
  218.       return(NULL) ;
  219.    l = strlen(s) ;
  220.    if (nextstring + l >= maxstring)
  221.       error("! out of string space") ;
  222.    (void)strcpy(nextstring, s) ;
  223.    s = nextstring ;
  224.    nextstring += l + 1 ;
  225.    return(s) ;
  226. }
  227. /*
  228.  *   Finally, our main routine.
  229.  */
  230. #ifdef VMS
  231. main()
  232. #else
  233. void main(argc, argv)
  234.     int argc ;
  235.     char *argv[] ;
  236. #endif
  237. {
  238.    extern void exit() ;
  239.    int i, lastext = -1 ;
  240.    register sectiontype *sects ;
  241.    int noenv = 0 ; /* do we read PRINTER or not? */
  242.  
  243. #ifdef VMS
  244.    progname = &thh[0] ;
  245.    strcpy(progname,"DVIPS%ERROR");
  246. #else
  247.    progname = argv[0] ;
  248. /* we sneak a look at the first arg in case it's debugging */
  249. #ifdef DEBUG
  250.    if (argc > 1 && strncmp(argv[1], "-d", 2)==0) {
  251.       if (sscanf(argv[1]+2, "%d", &debug_flag)==0)
  252.          debug_flag = 0 ;
  253.    }
  254. #endif
  255. #endif
  256.    initialize() ;
  257.    checkenv(0) ;
  258.    getdefaults(CONFIGFILE) ;
  259.    getdefaults((char *)0) ;
  260. /*
  261.  *   This next whole big section of code is straightforward; we just scan
  262.  *   the options.  An argument can either immediately follow its option letter
  263.  *   or be separated by spaces.  Any argument not preceded by '-' and an
  264.  *   option letter is considered a file name; the program complains if more
  265.  *   than one file name is given, and uses stdin if none is given.
  266.  */
  267. #ifdef VMS
  268. vmscli();
  269. #else
  270.    for (i=1; i<argc; i++) {
  271.       if (*argv[i]=='-') {
  272.          char *p=argv[i]+2 ;
  273.          char c=argv[i][1] ;
  274.          switch (c) {
  275. case 'c' :
  276.             if (*p == 0 && argv[i+1])
  277.                p = argv[++i] ;
  278.             if (sscanf(p, "%d", &numcopies)==0)
  279.                error("! Bad number of copies option (-c).") ;
  280.             break ;
  281. case 'd' :
  282. #ifdef DEBUG
  283.         if (*p == 0 && argv[i+1])
  284.            p = argv[++i];
  285.         if (sscanf(p, "%d", &debug_flag)==0)
  286.            error("! Bad debug option (-d).");
  287.         break;
  288. #else
  289.             error("not compiled in debug mode") ;
  290.             break ;
  291. #endif /* DEBUG */
  292. case 'e' :
  293.             if (*p == 0 && argv[i+1])
  294.                p = argv[++i] ;
  295.             if (sscanf(p, "%d", &maxdrift)==0 || maxdrift<0)
  296.                error("! Bad maxdrift option (-e).") ;
  297.         vmaxdrift = maxdrift;
  298.             break ;
  299. case 'f' :
  300.             filter = (*p != '0') ;
  301.             if (filter)
  302.                oname = "" ;
  303.             noenv = 1 ;
  304.             break ;
  305. case 'h' : case 'H' :
  306.             if (*p == 0 && argv[i+1])
  307.                p = argv[++i] ;
  308.             if (strcmp(p, "-") == 0)
  309.                headers_off = 1 ;
  310.             else
  311.                (void)add_header(p) ;
  312.             break ;
  313. case 'm' :
  314.             manualfeed = (*p != '0') ;
  315.             break ;
  316. case 'n' :
  317.             if (*p == 0 && argv[i+1])
  318.                p = argv[++i] ;
  319. #ifdef SHORTINT
  320.             if (sscanf(p, "%ld", &maxpages)==0)
  321. #else    /* ~SHORTINT */
  322.             if (sscanf(p, "%d", &maxpages)==0)
  323. #endif    /* ~SHORTINT */
  324.                error("! Bad number of pages option (-n).") ;
  325.             break ;
  326. case 'o' : case 'O' :
  327.             if (*p == 0 && argv[i+1] && *argv[i+1]!='-')
  328.                p = argv[++i] ;
  329.             oname = p ;
  330.             noenv = 1 ;
  331.             break ;
  332. case 'p' :
  333.             if (*p == 0 && argv[i+1])
  334.                p = argv[++i] ;
  335. #ifdef SHORTINT
  336.             switch(sscanf(p, "%d.%d", &firstpage, &firstseq)) {
  337. #else    /* ~SHORTINT */
  338.             switch(sscanf(p, "%ld.%ld", &firstpage, &firstseq)) {
  339. #endif    /* ~SHORTINT */
  340. case 1:        firstseq = 0 ;
  341. case 2:        break ;
  342. default:
  343.                error("! Bad first page option (-p).") ;
  344.             }
  345.             notfirst = 1 ;
  346.             break ;
  347. case 'l':
  348.             if (*p == 0 && argv[i+1])
  349.                p = argv[++i] ;
  350. #ifdef SHORTINT
  351.             switch(sscanf(p, "%d.%d", &lastpage, &lastseq)) {
  352. #else    /* ~SHORTINT */
  353.             switch(sscanf(p, "%ld.%ld", &lastpage, &lastseq)) {
  354. #endif    /* ~SHORTINT */
  355. case 1:        lastseq = 0 ;
  356. case 2:        break ;
  357. default:
  358.                error("! Bad last page option (-p).") ;
  359.             }
  360.             notlast = 1 ;
  361.             break ;
  362. case 'q' : case 'Q' :
  363.             quiet = (*p != '0') ;
  364.             break ;
  365. case 'r' :
  366.             reverse = (*p != '0') ;
  367.             break ;
  368. case 't' :
  369.             if (*p == 0 && argv[i+1])
  370.                p = argv[++i] ;
  371.             if (strcmp(p, "landscape") == 0)
  372.                landscape = 1;
  373.             else
  374.                paperfmt = p ;
  375.             break ;
  376. case 'x' :
  377.             if (*p == 0 && argv[i+1])
  378.                p = argv[++i] ;
  379.             if (sscanf(p, "%d", &mag)==0 || mag < 10 ||
  380.                        mag > 100000)
  381.                error("! Bad magnification parameter (-x).") ;
  382.             overridemag = 1 ;
  383.             break ;
  384. case 'C' :
  385.             if (*p == 0 && argv[i+1])
  386.                p = argv[++i] ;
  387.             if (sscanf(p, "%d", &collatedcopies)==0)
  388.                error("! Bad number of collated copies option (-C).") ;
  389.             break ;
  390. case 'D' :
  391.             if (*p == 0 && argv[i+1])
  392.                p = argv[++i] ;
  393.             if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  394.                        actualdpi > 10000)
  395.                error("! Bad dpi parameter (-D).") ;
  396.         vactualdpi = actualdpi;
  397.             break ;
  398. case 'K' :
  399.             removecomments = (*p != '0') ;
  400.             break ;
  401. case 'U' :
  402.             nosmallchars = (*p != '0') ;
  403.             break ;
  404. case 'X' :
  405.             if (*p == 0 && argv[i+1])
  406.                p = argv[++i] ;
  407.             if (sscanf(p, "%d", &actualdpi)==0 || actualdpi < 10 ||
  408.                        actualdpi > 10000)
  409.                error("! Bad dpi parameter (-D).") ;
  410.             break ;
  411. case 'Y' :
  412.             if (*p == 0 && argv[i+1])
  413.                p = argv[++i] ;
  414.             if (sscanf(p, "%d", &vactualdpi)==0 || vactualdpi < 10 ||
  415.                        vactualdpi > 10000)
  416.                error("! Bad dpi parameter (-D).") ;
  417.         vactualdpi = vactualdpi;
  418.             break ;
  419. case 'F' :
  420.             sendcontrolD = (*p != '0') ;
  421.             break ;
  422. case 'M':
  423.             dontmakefont = 1 ;
  424.             break ;
  425. case 'N' :
  426.             disablecomments = (*p != '0') ;
  427.             break ;
  428. case 'P' :
  429.             if (*p == 0 && argv[i+1])
  430.                p = argv[++i] ;
  431.             printer = p ;
  432.             noenv = 1 ;
  433.             getdefaults("") ;
  434.             break ;
  435. case 'R' :
  436.             reverse = 0 ;
  437.             break ;
  438. case 's' :
  439.             safetyenclose = (*p != '0') ;
  440.             break ;
  441. case 'Z' :
  442.             compressed = (*p != '0') ;
  443.             break ;
  444. case '?' :
  445.             (void)fprintf(stderr, banner) ;
  446.             break ;
  447. default:
  448.             error("! Bad option, not one of cefhlmnopqrtxCDFKNPUXYZ?") ;
  449.          }
  450.       } else {
  451.          if (*iname == 0) {
  452.             register char *p ;
  453.  
  454.             lastext = 0 ;
  455.             iname = nextstring ;
  456.             p = argv[i] ;
  457.             while (*p) {
  458.                *nextstring = *p++ ;
  459.                if (*nextstring == '.')
  460.                   lastext = nextstring - iname ;
  461.                else if (*nextstring == '/' || *nextstring == ':')
  462.                   lastext = 0 ;
  463.                nextstring++ ;
  464.             }
  465.             if (lastext == 0) {
  466.                lastext = nextstring - iname ;
  467.                *nextstring++ = '.' ;
  468.                *nextstring++ = 'd' ;
  469.                *nextstring++ = 'v' ;
  470.                *nextstring++ = 'i' ;
  471.             }
  472.             *nextstring++ = 0 ;
  473.          } else
  474.             error("! Two input file names specified.") ;
  475.       }
  476.    }
  477.    if (noenv == 0) {
  478.       register char *p ;
  479.       extern char *getenv() ;
  480.       if (p = getenv("PRINTER")) {
  481.          strcpy(nextstring, "config.") ;
  482.          strcat(nextstring, p) ;
  483.          getdefaults(nextstring) ;
  484.       }
  485.    }
  486. #endif
  487.    checkenv(1) ;
  488.    if (!quiet)
  489.       (void)fprintf(stderr, banner) ;
  490.    if (oname[0] == '-' && oname[1] == 0)
  491.       oname[0] = 0 ;
  492.    if (*oname == 0 && ! filter) {
  493.       oname = nextstring ;
  494.       for (i=0; i<=lastext; i++)
  495.          *nextstring++ = iname[i] ;
  496.       *nextstring++ = 'p' ;
  497.       *nextstring++ = 's' ;
  498.       *nextstring++ = 0 ;
  499.    }
  500. #ifdef DEBUG
  501.    if (dd(D_PATHS)) {
  502. #ifdef SHORTINT
  503.     (void)fprintf(stderr,"input file %s output file %s swmem %ld\n",
  504. #else /* ~SHORTINT */
  505.        (void)fprintf(stderr,"input file %s output file %s swmem %d\n",
  506. #endif /* ~SHORTINT */
  507.            iname, oname, swmem) ;
  508.    (void)fprintf(stderr,"tfm path %s\npk path %s\n", tfmpath, pkpath) ;
  509.    (void)fprintf(stderr,"fig path %s\nvf path %s\n", figpath, vfpath) ;
  510.    (void)fprintf(stderr,"config path %s\nheader path %s\n", 
  511.                   configpath, headerpath) ;
  512. #ifdef FONTLIB
  513.    (void)fprintf(stderr,"fli path %s\nfli names %s\n", flipath, fliname) ;
  514. #endif
  515.    } /* dd(D_PATHS) */
  516. #endif /* DEBUG */
  517. /*
  518.  *   Now we try to open the dvi file.
  519.  */
  520.    if (warningmsg)
  521.       error(warningmsg) ;
  522.    headerfile = (compressed? CHEADERFILE : HEADERFILE) ;
  523.    (void)add_header(headerfile) ;
  524.    if (*iname != 0)
  525.       dvifile = fopen(iname, READBIN) ;
  526.    else if (filter)
  527.       dvifile = stdin;
  528.    else
  529.       error("! No input filename supplied.") ;
  530.    if (dvifile==NULL)
  531.       error("! DVI file can't be opened.") ;
  532. #ifdef FONTLIB
  533.    fliload();    /* read the font libaries */
  534. #endif
  535. /*
  536.  *   Now we do our main work.
  537.  */
  538.    if (maxdrift < 0) {
  539.       if (actualdpi <= 599)
  540.          maxdrift = actualdpi / 100 ;
  541.       else if (actualdpi < 1199)
  542.          maxdrift = actualdpi / 200 + 3 ;
  543.       else
  544.          maxdrift = actualdpi / 400 + 6 ;
  545.    }
  546.    if (vmaxdrift < 0) {
  547.       if (vactualdpi <= 599)
  548.          vmaxdrift = vactualdpi / 100 ;
  549.       else if (vactualdpi < 1199)
  550.          vmaxdrift = vactualdpi / 200 + 3 ;
  551.       else
  552.          vmaxdrift = vactualdpi / 400 + 6 ;
  553.    }
  554.    prescanpages() ;
  555.    if (includesfonts)
  556.       (void)add_header(IFONTHEADER) ;
  557.    if (usesPSfonts)
  558.       (void)add_header(PSFONTHEADER) ;
  559.    if (usesspecial)
  560.       (void)add_header(SPECIALHEADER) ;
  561.    sects = sections ;
  562.    if (sects == NULL || sects->next == NULL) {
  563.       sectioncopies = collatedcopies ;
  564.       collatedcopies = 1 ;
  565.    } else {
  566.       totalpages *= collatedcopies ;
  567.       multiplesects = 1 ;
  568.    }
  569.    initprinter() ;
  570.    outbangspecials() ;
  571.    for (i=0; i<collatedcopies; i++) {
  572.       sects = sections ;
  573.       while (sects != NULL) {
  574.          if (! quiet) {
  575.             if (prettycolumn > 77) {
  576.                fprintf(stderr, "\n") ;
  577.                prettycolumn = 0 ;
  578.             }
  579.             (void)fprintf(stderr, ". ") ;
  580.             prettycolumn += 2 ;
  581.          }
  582.          (void)fflush(stderr) ;
  583.          dosection(sects, sectioncopies) ;
  584.          sects = sects->next ;
  585.       }
  586.    }
  587.    cleanprinter() ;
  588.    if (! quiet)
  589.       (void)fprintf(stderr, "\n") ;
  590.    exit(0) ;
  591.    /*NOTREACHED*/
  592. }
  593. #ifdef VMS
  594. #include "vms/vmscli.c"
  595. #endif
  596. @
  597.